<template>
  <div>
    <QueryForm v-model="filters" :query-form-config="queryFormConfig" @handleQuery="getList(1)" @clear="clear">
      <template v-if="meta.indexOf('OnlineFoodEatGoodsOrderListExcel') > -1" #btns>
        <el-button :loading="exportLoading" type="primary" @click="batchexport">{{ $t('message.daochu') }}</el-button>
      </template>
    </QueryForm>
    <div v-if="tjList && tjList.length > 0" class="total_wrapc">
      <div class="first item">
        <div class="key">{{ $t('message.shanxuan') }}</div>
        <div class="key">{{ $t('message.heji') }}</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.payMethod }}</div>
          <div class="value">
            <!-- <span>{{ $t('message.rmbfh') }}</span> -->
            <span class="price">{{ item.money }}</span>
          </div>
        </div>
      </div>
      <el-tooltip effect="dark" placement="left">
        <div class="df_ac"><img style="width: 16px;" src="@/assets/icon/question.png">{{ $t('message.jesm') }}</div>
        <div slot="content" v-html="smTip" />
      </el-tooltip>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit :header-row-class-name="'table-header'" class="doumee-element-table">
      <el-table-column align="center" :label="$t('message.ct')" prop="ctName" min-width="210" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.cpmc')" prop="goodName" min-width="140" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.cpdj')" prop="price" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.xssl')" prop="saleNum" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.sfk')" prop="realPrice" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.ytk')" prop="refundPrice" min-width="110" show-overflow-tooltip />
      <el-table-column align="center" :label="$t('message.zffs')" min-width="120" show-overflow-tooltip prop="payMethodName" />
      <el-table-column align="center" :label="$t('message.ddh')" prop="orderId" min-width="220" />
      <el-table-column align="center" :label="$t('message.ddly')" min-width="110" prop="channelTypeName" />
      <el-table-column align="center" :label="$t('message.memberInfo')" min-width="240" show-overflow-tooltip prop="userInfo" />
      <el-table-column align="center" :label="$t('message.createTime')" prop="createTime" min-width="160" />
      <el-table-column align="center" :label="$t('message.czy')" prop="creatorName" min-width="120" show-overflow-tooltip />
    </el-table>
    <Pagination :total="total" :page="pagination.page" @current-change="getList" @SizeChange="handleSizeChange" />
  </div>
</template>

<script>
import { getChannelType, goodsRestaurantList2On, configList, eatGoodsOrderListExcel, eatGoodOrderDetials } from '@/api'
import { parseTime } from '@/utils'
import dayjs from 'dayjs'
import QueryForm from '@/components/QueryForm'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'SetmealDetail',
  components: {
    Pagination, QueryForm
  },
  data () {
    return {
      meta: [],
      queryFormConfig,
      venueList: [],
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      exportLoading: false,
      payTypes: [],
      listLoading: false,
      tjList: [],
      filters: {},
      smTip: ''
    }
  },
  created () {
    this.meta = this.$route.meta.buttons || []
    this.init()
    this.getConfigList()
  },
  methods: {
    getChannel () {
      getChannelType({ param: { code: 'CHANNEL_GOODS' } }).then(res => {
        this.queryFormConfig.formItems[5].options = res.recordList.map(item => {
          return {
            label: item.name,
            value: item.value
          }
        })
      })
    },
    init () {
      let time = []
      const start = new Date()
      const end = new Date()
      start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
      time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
      this.$set(this.filters, 'time', time)
    },
    getLoading () {
      this.goodsRestaurantList()
      this.getList(1)
    },
    getConfigList () {
      configList({ param: { type: '2', code: '1' } })
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.payTypes = res.recordList
          const arr = res.recordList
          this.queryFormConfig.formItems[3].options = arr
        })
        .catch(() => { })
    },
    nameWithPayMethod (payM) {
      const temp = this.payTypes.find((item) => item.value === payM)
      return temp ? temp.name : '-'
    },
    // 获取列表
    goodsRestaurantList () {
      goodsRestaurantList2On({ param: { venueId: sessionStorage.getItem('venueId'), type: 1, } })
        .then(res => {
          if (res.errorCode === '000000') {
            this.ctList = res.recordList || []
            this.queryFormConfig.formItems[0].options = res.recordList || []
          }
        })
        .catch(() => { })
    },
    handleSizeChange (value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList (page) {
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (filters.time && filters.time.length > 0) {
        filters.startDate = filters.time[0]
        filters.endDate = filters.time[1]
      } else {
        filters.startDate = ''
        filters.endDate = ''
      }
      filters.type = '2'
      pagination.firstQueryTime = parseTime(new Date())
      this.listLoading = true
      eatGoodOrderDetials({ pagination, param: { ...filters } }).then(res => {
        this.listLoading = false
        if (res.errorCode !== '000000') return
        this.list = res.recordList
        this.total = res.totalCount
        const tjList = res.data
        if (tjList) {
          let smTip = ''
          tjList.forEach(s => {
            smTip = smTip + s.tip + '<br />'
          })
          this.tjList = tjList
          this.smTip = smTip
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    clear () {
      this.filters = {
        time: this.filters.time
      }
      this.getList(1)
    },
    batchexport () {
      const { filters } = this
      if (filters.time && filters.time.length > 0) {
        filters.startDate = filters.time[0]
        filters.endDate = filters.time[1]
      } else {
        filters.startDate = ''
        filters.endDate = ''
      }
      this.exportLoading = true
      eatGoodsOrderListExcel({ param: { ...filters } }).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>
